package com.ricohgag.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ricohgag.entity.ShopAd;
import com.ricohgag.mapper.ShopAdMapper;
import com.ricohgag.service.ShopAdService;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 肖晖
 * @since 2018-08-25
 */
@Service
public class ShopAdServiceImpl extends ServiceImpl<ShopAdMapper, ShopAd> implements ShopAdService {
    @Autowired
    private ShopAdMapper shopAdMapper;

    @Override
    public IPage<ShopAd> selectShopAdPage(String name, Page<ShopAd> page) throws Exception{
        QueryWrapper<ShopAd> qw = new QueryWrapper<>();
        if(name != null){
            qw.like("name", name);
        }

        IPage<ShopAd> shopAdIPage = shopAdMapper.selectPage(page, qw);
        Assert.assertSame(page, shopAdIPage);

        return shopAdIPage;

    }

    @Override
    public ShopAd seleShopAdOne(Integer id) throws Exception{

        return shopAdMapper.selectById(id);
    }

    @Override
    public int updateShopAd(ShopAd shopAd) throws Exception{
        return shopAdMapper.updateById(shopAd);
    }

    @Override
    public int insertShopAd(ShopAd shopAd) throws Exception{
        return shopAdMapper.insert(shopAd);
    }

    @Override
    public int deleteShopAd(int id) throws Exception{
        return shopAdMapper.deleteById(id);
    }

    @Override
    public List<ShopAd> selectShopAdAll() throws Exception{
        QueryWrapper<ShopAd> qw = new QueryWrapper<>();
        Date now = new Date();
        qw.lt("start_time", now);
        qw.gt("end_time", now);
        return shopAdMapper.selectList(qw);

    }
}
